package com.gome.oa.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import com.gome.oa.custom.CustomCheckbox;
import com.gome.oa.custom.GMBrowser;
import com.gome.oa.dialog.FillFormDialog;
import com.gome.oa.model.Day;
import com.gome.oa.model.SelectType;
import com.gome.oa.util.DateUtil;

/**
 *
 * @author: tang li
 * @date: 2016年8月22日
 * @desc: 
 *
 */
public class ConfirmActionListener implements ActionListener {
	
	private FillFormDialog fillFormDialog;
	
	private DefaultTableModel dtm;
	
	private JTable tblForm;
	
	private GMBrowser browser;

	public ConfirmActionListener(FillFormDialog fillFormDialog, JTable tblForm, GMBrowser browser) {
		super();
		this.fillFormDialog = fillFormDialog;
		this.tblForm = tblForm;
		this.browser = browser;
		this.dtm = (DefaultTableModel)tblForm.getModel();
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (tblForm.isEditing())
			tblForm.getCellEditor().stopCellEditing();
		if(validateForm()) {
			fillIntoBrowsesr();
			fillFormDialog.dispose();
		}
	}
	
	private boolean validateForm() {
		for(int i = 0; i < dtm.getRowCount() - 1; i++) {
			CustomCheckbox customCheckbox = (CustomCheckbox)dtm.getValueAt(i, 0);
			if(!customCheckbox.isSelected()) {
				continue;
			}
			if(!customCheckbox.isVisableInRow()) {
				continue;
			}
			
			Day day = customCheckbox.getDay();
			if(day.getHour() < 1) {
				JOptionPane.showConfirmDialog(fillFormDialog, "(" + DateUtil.format1(day.getMinDate()) + ")加班小时必须大于0", "信息提示", JOptionPane.DEFAULT_OPTION);
				return false;
			}
			String comment = (String)dtm.getValueAt(i, 6);
			if(comment == null || comment.trim().equals("")) {
				JOptionPane.showConfirmDialog(fillFormDialog, "(" + DateUtil.format1(day.getMinDate()) + ")加班事由不能为空", "信息提示", JOptionPane.DEFAULT_OPTION);
				return false;
			}
		}
		return true;
	}
	
	private void fillIntoBrowsesr() {
		//reset flag value
		browser.setAllowExecJs(true);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				for(int i = 0; i < dtm.getRowCount() - 1; i++) {
					CustomCheckbox customCheckbox = (CustomCheckbox)dtm.getValueAt(i, 0);
					if(!customCheckbox.isSelected()) {
						continue;
					}
					if(!customCheckbox.isVisableInRow()) {
						continue;
					}
					String startDate = DateUtil.format1(customCheckbox.getDay().getMinDate());
					//提示申请不能超过总时数36小时等消息后，就不再执行javascript
					JComboBox<?> combox = (JComboBox<?>)dtm.getValueAt(i, 1);
					String value = ((SelectType)combox.getSelectedItem()).getValue();
					String startTime = DateUtil.format3((Date)dtm.getValueAt(i, 3));
					browser.executeScript2("document.getElementById('TypeOfOvertime').selectedIndex=" + value + ";");
					browser.executeScript2("document.getElementById('StartDate').value='" + startDate + "';");
					browser.executeScript2("document.getElementById('StartTime').value='" + startTime + "';");
					browser.executeScript2("document.getElementById('EndTime').value='" + dtm.getValueAt(i, 4) + "';");
					browser.executeScript2("AStartTimeAEndTime();");
					browser.executeScript2("document.getElementById('SubjectToWorkOvertime').value='" + dtm.getValueAt(i, 6) + "';");
					browser.executeScript2("onAddInvoice();");
				}
			}
		});
	}

}
